﻿using System;
using Firefly.Framework.Input;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Firefly.Framework.Windows.Forms
{
    public class Checkbox : Control
    {
        public event EventHandler CheckedChanged;

        protected bool _checked = false;

        [ContentSerializer(Optional = true)]
        public virtual bool Checked
        {
            get { return _checked; }
            set { SetChecked(value); }
        }

        protected virtual void SetChecked(bool value)
        {
            if (_checked != value)
            {
                _checked = value;
                OnCheckecChanged();
            }
        }

        protected virtual void OnCheckecChanged()
        {
            NotifyCheckedChanged();
        }

        protected override void OnMouseDown(MouseButtonMap map, MouseButtonMapEventArgs e)
        {
            Focus();
            Checked = !Checked;
            base.OnMouseDown(map, e);
        }

        protected override void OnKeyDown(KeyMap map, KeyMapEventArgs e)
        {
            switch (map.MappedButtons[0])
            {
                case Keys.Space:
                    Checked = !Checked;
                    break;
                case Keys.Tab:
                    if (InputService.Instance.KeyboardManager.IsButtonDown(Keys.LeftShift) ||
                        InputService.Instance.KeyboardManager.IsButtonDown(Keys.RightShift))
                    {
                        DoTabPrev();
                    }
                    else
                    {
                        DoTabNext();
                    }
                    break;
                default:
                    Parent.RaiseBubbleEvent(this, new BubbleEventArgs(BubbleEvent.KeyDown, map, e));
                    break;
            }

            NotifyKeyDown(e);
        }

        protected void NotifyCheckedChanged()
        {
            EventHandler handler = CheckedChanged;

            if (handler != null)
            {
                handler(this, null);
            }
        }

        #region IPipelineSerializeable Members

        public override void DeserializeContent(ContentReader input)
        {
            base.DeserializeContent(input);
            _checked = input.ReadBoolean();
        }

        #endregion

        #region IDrawable Members

        public override void Draw(GameTime gameTime)
        {
            if (Visible)
            {
                if (_checked)
                {
                    WindowsService.Instance.Theme.Toggle.Draw("CheckTrue", DrawRectangle, Color);
                }
                else
                {
                    WindowsService.Instance.Theme.Toggle.Draw("CheckFalse", DrawRectangle, Color);
                }

                DrawChildren(gameTime);
            }
        }

        #endregion
    }
}
